package com.lanting.meetingserver.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanting.meetingserver.domain.User;
import com.lanting.meetingserver.service.impl.UserServiceImpl;
import com.lanting.meetingserver.util.Common;
import com.lanting.meetingserver.util.CookieHelper;

@Controller
@RequestMapping("/Login")
public class LoginController {
	
	@Resource
	private UserServiceImpl userService;
	
	
	
	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}
	
	@RequestMapping("login")
	public String login2(){
		return "/";
	}
	
	@RequestMapping("exit")
	public String exit(HttpServletResponse response,HttpServletRequest request) throws IOException{
		CookieHelper.DeleteCookie(response);
		response.sendRedirect(request.getContextPath());
		return null;
	}



	@RequestMapping(value="login",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request,HttpServletResponse response){
		
		String loginId = request.getParameter("loginId");
		String password = request.getParameter("password");
		User user = null;
		String md5Pwd = Common.getSecurityPassword(password, loginId);
		user = userService.login(loginId, md5Pwd);
		Map<String, Object> map2 = new HashMap<String, Object>();
		if (user != null) {
			response.addCookie(CookieHelper.createCookie(user));
			map2.put("success",true);
			map2.put("url","Index/index");
		}else {
			map2.put("success", false);
			map2.put("data","登录失败！");
		}
		return map2;
		
	}
}
